﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private double getPacket(ref double total,int n)
        {
            if (n == 1) return total;
            Random rnd = new Random();
            double avg, thisPacket;
            avg = Math.Round(total / n, 2);
            thisPacket = avg/2 +avg *rnd.Next(101)/100;
            thisPacket=Math.Round(thisPacket,2);
            total = thisPacket;
            total = Math.Round(total, 2);
            return thisPacket;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            double total = int.Parse(textBox1.Text);
            int n = int.Parse(textBox2.Text); 
            double[] redPacket = new double[n];
            for(int i = n; i>0; i--)
            {
                redPacket[n-i] = getPacket(ref total,i);
            }
            textBox3.Text = string.Join("\t", redPacket);
            textBox4.Text = sumPacket(redPacket).ToString();    
        }
        private double sumPacket(double[] redPacket)
        {
            double sum = 0;
            foreach (double d in redPacket)
                sum += d;
            return sum;
        }

    }
}
